#include <QPixmap>
#include <QPainter>
#include <QSize>
#include <QMap>

#include "styleiddelegate.h"
#include "appmanager.h"

StyleIdDelegate::StyleIdDelegate(const QMap<int, Style> &styles, const bool isForTree, QObject *parent) :
  QStyledItemDelegate(parent),
  //    mFallStyleImg(NULL),
  mStyles(styles),
  mIsForTree(isForTree)
{

  mFallStyleImg = NULL;
}

void StyleIdDelegate::paint(QPainter *painter,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index) const
{
  if (mIsForTree && index.parent() == QModelIndex())
    {
      return;
    }
  if( (appManager->stylesLoaded() == AllStylesLoaded
       || appManager->stylesLoaded() == SomeStylesLoaded)
      && index.data().canConvert<int>()
      )
    {
      int num = index.data().toInt(); // no check here
      if (num!=0)
        {
          Q_ASSERT(num>0);
          // preparing icon
          QPixmap icon;

          if (appManager->mStyles.contains(num))
            {
              // got correct style
              icon.load(appManager->mStyles.value(num).img);
              icon =  icon.scaledToHeight( 2*option.fontMetrics.height() - 1,
                                           Qt::SmoothTransformation);

              QPoint tl(option.rect.topLeft());

              painter->save();
              painter->drawPixmap(tl, icon);

              QRect txtRect = option.rect;

              txtRect.setTopLeft(QPoint(txtRect.topLeft().x() + 3 + icon.width(),
                                        txtRect.topLeft().y()));
              icon.load(appManager->mStyles.value(num).img); //!
              painter->drawText(txtRect, Qt::TextWordWrap | Qt::AlignLeft | Qt::AlignVCenter,
                                appManager->mStyles.value(num).name); // !

              painter->restore();
            }
          else
            {
              // no such style, load fallback pic
              if (mFallStyleImg!=NULL)
                painter->drawPixmap(QPointF(0.0, 0.0), *mFallStyleImg);
            }
        }
      else
        {
          // case when style == 0
          if (mFallStyleImg!=NULL)
            painter->drawPixmap(QPointF(0.0, 0.0), *mFallStyleImg);
        }
    }
  else
    QStyledItemDelegate::paint(painter, option, index);
}

QSize StyleIdDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  // TODO: check size!!!
  bool converted = false;

  int num = index.data().toInt(&converted);
  if (converted)
    {
      Q_ASSERT(num>=0);
      int w = option.fontMetrics.width(appManager->mStyles.value(num).name);
      int h = option.fontMetrics.height();
      //        qDebug()<<"w="<<w<<"\th="<<h;
      return QSize(w, h*2);
    }
  return QSize(24, 24);
}

void StyleIdDelegate::generateFallStyleImg()
{
  mFallStyleImg = new QPixmap(24, 24);

  QPainter painter;
  painter.begin(mFallStyleImg);

  painter.setPen(Qt::SolidLine);
  painter.drawLine(mFallStyleImg->rect().topLeft(),
                   mFallStyleImg->rect().bottomRight());
  painter.drawLine(mFallStyleImg->rect().topRight(),
                   mFallStyleImg->rect().bottomLeft());
  painter.end();
}



